// 参考：https://segmentfault.com/a/1190000020812860
// @ts-nocheck
import React from "react";
import { BrowserRouter/* , HashRouter */ as Router, Switch, Route, Redirect, Link, useRouteMatch, useParams } from 'react-router-dom';
import routes from './routes'

const handleRoutes = (routes) => {
  return (
    <Switch>
      {routes.map(({ component, redirect, path, key = path, childRoutes }) => {
        if (childRoutes) {
          const Layout = component
          return (
            <Layout key={key}>
              {handleRoutes(childRoutes)}
            </Layout>
          )
        }
        return redirect
          ? (
            <Route exact path={path} key={key}>
              <Redirect to={redirect} />
            </Route>
          )
          : <Route exact path={path} component={component} key={key} />
      })}
    </Switch>
  )
}

export default () => {
  return (
    <Router>
      {handleRoutes(routes)}
    </Router>
  )
}
